﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Control.CheckForIllegalCrossThreadCalls = true;
            InitializeComponent();
            cmbsize.Text = "100";
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtclientcount.Text))
            {
                MessageBox.Show("链接数不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtipaddres.Text))
            {
                MessageBox.Show("ip地址不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtport.Text))
            {
                MessageBox.Show("端口不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtinitcount.Text))
            {
                MessageBox.Show("初始连接数不能为空！");
                return;
            }
            txtclientcount.Enabled = false;
            txtinitcount.Enabled = false;
            cmbss.Enabled = false;
            cmbzcount.Enabled = false;
            txtipaddres.Enabled = false;
            txtport.Enabled = false;
            var initcount = Convert.ToInt32(txtinitcount.Text);
            var linkcount = Convert.ToInt32(txtclientcount.Text);
            var ip = txtipaddres.Text;
            var port = Convert.ToInt32(txtport.Text);
            var packagesize = Convert.ToInt32(cmbsize.Text);
            PublicData.PackageData = GetPackage(packagesize);

            ClientService cs = new ClientService(linkcount, initcount, ip, port);
            Thread client = new Thread(()=>cs.OpenClient());
            client.Start();
            if(!string.IsNullOrEmpty(cmbzcount.Text)){
                 var zcount=Convert.ToInt32(cmbzcount.Text);
                 var time = string.IsNullOrEmpty(cmbss.Text) ? 1 : Convert.ToInt32(cmbss.Text);
                 Thread lazy = new Thread(() => LazyClient(cs, initcount, linkcount, zcount, time));
                lazy.Start();
            }
            
        }

        private void LazyClient(ClientService cs,int initcount,int linkcount,int zcount,int ss)
        {
            var jc = (linkcount - initcount) / zcount;
            for (int i = 0; i < jc; i++)
            {
                Thread.Sleep(ss * 1000);
                cs.AddClient(linkcount - initcount);
            }
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnstop_Click(object sender, EventArgs e)
        {
            txtclientcount.Enabled = true;
            txtinitcount.Enabled = true;
            cmbss.Enabled = true;
            cmbzcount.Enabled = true;
            txtipaddres.Enabled = true;
            txtport.Enabled = true;
        }

        private string GetPackage(int size)
        {
            StringBuilder txt = new StringBuilder();
            //var data= File.ReadAllText(Application.StartupPath+"/data.txt");
            StringBuilder data = new StringBuilder();
            for (int j = 0; j < 1024; j++)
            {
                data.Append("1");
            }//1kb
            for (int i = 0; i < size; i++)
            {
                txt.Append(data);
            }
            return txt.ToString();
        }

        private void cmbsize_SelectedIndexChanged(object sender, EventArgs e)
        {
            var packagesize = Convert.ToInt32(cmbsize.Text);
            PublicData.PackageData = GetPackage(packagesize);
        }

    }
}
